var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");
var FANKA_CONST_AD_TIME = 40;
var fanka = {
  go_page_from_home() {
    if (ocr_utils.screen_exists_text_all(["百度", "听书"], true)) {
      console.log("fanka:go_page_from_home");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页福利"])
      );
      sleep(2 * 1000);
      this.init();
    } else {
      console.log("fanka:go_page_from_home: NOT FOUND");
    }
  },
  init() {
    in_task.init(null, null, null, null, null, null, 3);
    var ocr_text = ocr_utils.find_text(
      "翻卡赢",
      false,
      null,
      10,
      (device.height / 10) * 3
    );
    if (ocr_text) {
      console.log(
        "fanka init: 找到---" + utils.str_format(ocr_text.text) + "-"
      );
      var ocr_text = ocr_utils.screen_exists_text_one("开个", true);
      if (ocr_text) {
        utils.click_ocr_text(ocr_text);
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        this.run();
      }
    } else {
      this.after();
    }
  },
  watch_ad(ad_interval) {
    console.log("fanka watch_ad: " + ad_interval + "秒");
    var if_current_ad_is_over_interval = 2; //每隔2s检查当前广告是否已经结束
    var is_over_early = false;
    while (ad_interval > if_current_ad_is_over_interval) {
      if (utils.exists_text_starts_with("已获得")) {
        var before_over_interval = random(2, 5);
        console.log(
          "fanka watch_ad: 找到--已获得--，广告已结束,再过" +
            utils.str_format(before_over_interval) +
            "s后停止当前广告"
        );
        sleep(before_over_interval * 1000);
        is_over_early = true; //广告实际比预计的时间提前结束
        break;
      }
      sleep(if_current_ad_is_over_interval * 1000);
      ad_interval = ad_interval - if_current_ad_is_over_interval;
      console.log("fanka watch_ad: 广告时间预计还剩" + ad_interval + "秒");
    }
    if (is_over_early) {
      this.go_back();
      return;
    }
    if (ad_interval > 0) {
      console.log("fanka watch_ad: 广告时间还剩最后的" + ad_interval + "秒");
      sleep(ad_interval * 1000);
      this.go_back();
      return;
    }
  },
  go_back() {
    back();
    sleep(utils.get_random_interval(2, 0, 2, 1000));
    var ocr_text = ocr_utils.screen_exists_text_one("看视频可", true);
    if (ocr_text) {
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var ad_interval = utils.get_random_interval(FANKA_CONST_AD_TIME, 0, 5, 1);
      this.watch_ad(ad_interval);
    } else {
      console.log(
        "fanka go_back: 没找到-" + utils.str_format("看视频可再翻一次") + "-"
      );
      this.run();
    }
  },
  run() {
    var ocr_text = ocr_utils.screen_exists_text_one("看视频再", true);
    if (ocr_text) {
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var ad_interval = utils.get_random_interval(FANKA_CONST_AD_TIME, 0, 5, 1);
      this.watch_ad(ad_interval);
      return;
    } else {
      console.log("fanka run: 没找到-" + utils.str_format("看视频再") + "-");
      this.after();
    }
  },
  after() {
    console.log("fanka after");
    var ocr_text = ocr_utils.screen_exists_text_one("马上收下", true);
    if (ocr_text) {
      console.log("fanka after: 找到-" + utils.str_format("马上收下") + "-");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
    }
    this.next();
  },
  next() {
    console.log("fanka next");
  },
};
module.exports = fanka;
